import os
import json

class Config(object):
    def __init__(self, cfg_file_name):
        self.cfg_file_name = cfg_file_name
        self.cfg = {
            "SERVER" : {
                "SERVER_HOST":      "192.168.10.243",
                "TCP_SERVER_PORT":  9000,
                "UDP_SERVER_PORT":  14550,

                "TCP_SERVER_AUTO_TEST": False,
                "TCP_IMPORT_MAVLINK": False,
                "TCP_SEND_AUTO_ADDING": "",

                "UDP_SERVER_AUTO_TEST": False,
                "UDP_IMPORT_MAVLINK": False
            },

            "SIM_SERIAL_PORT": {
                "PORT": "COM42",
                "BAUD": 115200,
                "TCP_SERVER_HOST": "192.168.10.243",
                "TCP_SERVER_PORT": 9000
            }
        }
    
    def get_config(self):
        return self.cfg

    def read_config(self):
        f = None
        try:
            if os.path.exists(self.cfg_file_name):
                f = open(self.cfg_file_name, 'r')

                adata = f.read(-1)
                js = json.loads(adata)       
                self.cfg = js

                f.close()

                #self.show_config()
                return True
        except Exception as e:
            print("Read Config File Error: ", end="")
            print(e)
            if f is not None:
                f.close()
            return False
        
        return False

    def show_config(self):
        print("--------------------")
        print("Config Param:")

        print(json.dumps(self.cfg, indent=4))

        print("---------------------\n")

if __name__ == '__main__':
    cfg = Config("../config.json")
    cfg.read_config()
